package ru.ifmo.croak.impl.remoting.post.team2.server;

import ru.ifmo.croak.util.CroakFactory;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.logging.Logger;

/**
 * Created by Евгений
 * Date: 18.05.11
 * Time: 16:22
 */
public class PostServiceMain implements Runnable {
    private final String name;
    private final String providerName;
    private final Logger log;
    private final int port;
    public PostServiceMain (String name) {
        this.name = name;
        log = Logger.getLogger(name);
        port = Integer.parseInt(CroakFactory.getInstance().getProperty(name + ".post.port"));
        providerName = CroakFactory.getInstance().getProperty(name + ".post.provider");
    }

    public void run() {
        try {
            ServerSocket server = new ServerSocket(port, 0, InetAddress.getByName("localhost"));
            log.info(name + " service is started");

            while(true)
            {
                Thread thread = new Thread(new PostListener(providerName, server.accept()));
                thread.start();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            log.info(name + " service starting failed");
        }
    }

}
